// packageH/circleCommunity/topicDetails/topicDetails.js
var yz_pay = require("../../../mycomponent/yz_pay/yz_pay");
var app = getApp();
Page({
  behaviors: [yz_pay],
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    bannerindex: 1,
    mainCommentShow: false,
    replyshow: false,
    adminOperationshow: false,
    adminOperationInfo: [],
    releaseOperationshow: false,
    amountRewardshow: false,
    rewardPayshow: false,
    stickid: "",
    detailsData: {},
    StickCommentData: [],
    replyName: "",
    replyContent: "",
    host_comment_id: "",
    second_comment_id: "",
    maincomment: "",
    readertype: 1, // 1普通看帖人 2发帖人 3管理人
    is_praise: true,
    //打赏金额选项
    rewardArr: [
      {
        value: 1,
        name: "5",
      },
      {
        value: 2,
        name: "10",
      },
      {
        value: 3,
        name: "20",
      },
      {
        value: 4,
        name: "50",
      },
      {
        value: 5,
        name: "100",
      },
    ],
    rewardArrselect: 99999,
    rewardArrmoney: "",
    rewardArrmoneytype: 2,
    btnData: [],
    rewardMoney: 0,
    commentManagement: false,
    is_own: false,
    commend_id: "",
    mulcommentsshow: true,
    Height: 0,
    LastHeight: 0,
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    loading: false,
    replyheight: 0,

    reward_button: 0,

    popupSpecs: false,
    passwordInfo: {},
    pwds: [],
    pwd: "",
    pw1: "",
    pw2: "",
    pw3: "",
    pw4: "",
    pw5: "",
    pw6: "",
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    if (options.stickId) {
      this.data.stickid = options.stickId;
    } else {
      wx.navigateBack({
        delta: 1,
      });
    }
    this.getMicroDetails();
    this.getCircleSet();
    this.getStickComment();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync("langIndex");
    this.setData({ language: language.en });
  },

  bindChangeBannerIndex(evt) {
    this.setData({
      bannerindex: evt.detail.current + 1,
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  adminBtn(evt) {
    let type = evt.currentTarget.dataset.type;
    if (type == "del") {
      this.deleteBtn();
      return;
    } else if (type == "edit") {
      this.reEditBtn();
      return;
    }
    console.log(type);
    let dataJson = { circle_id: this.data.detailsData.circle_id, invitation_id: this.data.stickid, invitation_type: type };
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-invitation.set-invitation-review");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: dataJson,
      success: (resdata) => {
        let res = resdata.data;
        app.tips(res.msg);
        if (res.result != 1) return;
        let status = 0;
        if (this.data.detailsData[type] == 0) status = 1;
        let target = "detailsData." + type;
        this.setData({
          [target]: status,
          adminOperationshow: false,
        });
      },
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.loading) {
      console.log("没有更多数据");
    } else {
      this.getMoreData();
    }
  },

  /**
   * 朋友圈
   */
  onShareTimeline: function () {
    return {
      title: this.data.detailsData.title ? this.data.detailsData.title : "",
    };
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.detailsData.title ? this.data.detailsData.title : "",
      path:'/packageH/circleCommunity/topicDetails/topicDetails?stickId='+this.data.stickid
    };
  },
  amountRewardshowbtn() {
    this.setData({
      amountRewardshow: false,
    });
  },
  getMicroDetails() {
    let dataJson = {
      invitation_id: this.data.stickid,
    };
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-invitation.details");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: dataJson,
      success: (resdata) => {
        var res = resdata.data;
        if(res.data.content) res.data.content = res.data.content.replace(/(\r\n|\n|\r)/gm, "<br />");
        if (res.result == 1) {
          this.setData({
            detailsData: res.data,
          });
          if (this.data.detailsData) {
            if (this.data.detailsData.is_master == 1) {
              this.setData({
                readertype: 3,
              });
            } else if (this.data.detailsData.is_current_member == 1) {
              this.setData({
                readertype: 2,
              });
            }
            if (this.data.detailsData.is_manager == 1) {
              this.setData({
                is_praise: true,
              });
            } else {
              this.setData({
                is_praise: false,
              });
            }
          }
        } else {
          app.tips(res.msg);
        }
      },
      fail: function (res) {},
    });
  },
  pageInit() {
    this.data.page = 1;
    this.data.total_page = 0;
    this.data.isLoadMore = true;
    this.data.loading = false;
  },
  getStickComment() {
    this.pageInit();
    let dataJson = {
      invitation_id: this.data.stickid,
    };
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-invitation.get-comment-list");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: dataJson,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.data.total_page = res.data.last_page;
          this.setData({
            StickCommentData: res.data.data,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {},
    });
  },
  gotoCart() {
    wx.reLaunch({
      url: "/packageG/index/index",
    });
  },
  toShop(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/packageC/o2o/o2oStore/o2oStore?store_id=" + id,
    });
  },
  toGood(e) {
    let good_id = e.currentTarget.dataset.good_id;
    wx.navigateTo({
      url: "/packageA/detail_v2/detail_v2?id=" + good_id,
    });
  },
  attentionBtn() {
    if (this.data.detailsData.is_attention == 1) {
      this.cancelAttention();
    } else {
      this.addAttention();
    }
  },
  cancelAttention() {
    let dataJson = {
      attention_member_id: this.data.detailsData.member_id,
    };
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-member.cancel-attention");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: dataJson,
      success: (resdata) => {
        var res = resdata.data;
        app.tips(res.msg);
        if (res.result != 1) return;
        this.setData({
          "detailsData.is_attention": 0,
        });
      },
      fail: function (res) {},
    });
  },
  addAttention() {
    let dataJson = {
      attention_member_id: this.data.detailsData.member_id,
    };
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-member.attention-member");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: dataJson,
      success: (resdata) => {
        var res = resdata.data;
        app.tips(res.msg);
        if (res.result != 1) return;
        this.setData({
          "detailsData.is_attention": 1,
        });
      },
      fail: function (res) {},
    });
  },
  managementBtn() {
    let detailsData = this.data.detailsData;
    let adminOperationInfo = [];
    if (this.data.detailsData.is_master == 1) {
      adminOperationInfo = [
        {
          type: "is_top",
          img: `${this.data.imgPath}micro_communities/detail_roof@2x.png`,
          name: detailsData.is_top == 1 ? "取消置顶" : "置顶",
        },
        {
          type: "is_recommend",
          img: `${this.data.imgPath}micro_communities/detail_recommend@2x.png`,
          name: detailsData.is_recommend == 1 ? "取消推荐" : "推荐",
        },
        {
          type: "is_show",
          img: `${this.data.imgPath}micro_communities/detail_invisible@2x.png`,
          name: detailsData.is_show == 1 ? "取消未加入可见" : "未加入可见",
        },
      ];
    }

    if (this.data.detailsData.is_current_member == 1) {
      adminOperationInfo.push({
        type: "edit",
        img: `${this.data.imgPath}micro_communities/detail_revise@2x.png`,
        name: "修改",
      });
    }

    adminOperationInfo.push({
      type: "del",
      img: `${this.data.imgPath}micro_communities/detail_delect@2x.png`,
      name: "删除",
    });

    this.setData({
      adminOperationInfo,
      adminOperationshow: true,
    });
  },
  replyCommentsBtn(e) {
    let host_comment_id = e.currentTarget.dataset.hostcommentid;
    let second_comment_id = e.currentTarget.dataset.secondcommentid;
    let uname = e.currentTarget.dataset.uname;
    let content = e.currentTarget.dataset.content;
    let is_own = e.currentTarget.dataset.isown;
    this.setData({
      host_comment_id: host_comment_id,
      second_comment_id: second_comment_id,
      replyContent: "",
      replyName: uname,
      replyshowcontent: content,
    });
    console.log(this.data.replyContent);
    if (is_own == 1) {
      this.setData({
        is_own: true,
      });
    } else {
      this.setData({
        is_own: false,
      });
    }
    this.setData({
      commentManagement: true,
    });
  },
  praiseBtn(e) {
    let host_comment_id = e.currentTarget.dataset.hostcommentid;
    let second_comment_id = e.currentTarget.dataset.secondcommentid;
    let index = e.currentTarget.dataset.index;
    let chilindex = e.currentTarget.dataset.chilindex;
    let is_praise = e.currentTarget.dataset.ispraise;
    if (is_praise == 1) {
      return app.tips("你已赞过该贴");
    }
    this.getPraiseData(host_comment_id, second_comment_id, index, chilindex);
  },
  getPraiseData(host_comment_id, second_comment_id, index, chilindex) {
    let dataJson = {};
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-invitation.comment-like");

    dataJson.invitation_id = this.data.stickid;

    if (host_comment_id && second_comment_id) {
      dataJson.comment_id = second_comment_id;
    } else {
      dataJson.comment_id = host_comment_id;
    }
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: dataJson,
      success: (resdata) => {
        var res = resdata.data;
        app.tips(res.msg);
        if (res.result != 1) return;

        if (host_comment_id && second_comment_id) {
          this.setData({
            ["StickCommentData.[" + index + "].has_many_comment[" + chilindex + "].is_like"]: 1,
            ["StickCommentData.[" + index + "].has_many_comment[" + chilindex + "].link_count"]: this.data.StickCommentData[index].has_many_comment[chilindex].link_count
              ? this.data.StickCommentData[index].has_many_comment[chilindex].link_count + 1
              : 1,
          });
        } else {
          this.setData({
            ["StickCommentData[" + index + "].is_like"]: 1,
            ["StickCommentData[" + index + "].link_count"]: this.data.StickCommentData[index].link_count ? this.data.StickCommentData[index].link_count + 1 : 1,
          });
        }
      },
      fail: function (res) {},
    });
  },
  mulchMeatsShowBtn() {
    this.setData({
      mulcommentsshow: false,
    });
    this.getMoreData();
  },
  subMainCommentBtn() {
    this.setData({
      maincomment: "",
      mainCommentShow: true,
    });
  },
  moneyShow() {
    this.setData({
      rewardArrmoney: "",
      rewardArrselect: 9999,
      rewardMoney: 0,
      amountRewardshow: true,
    });
  },
  isPraiseBtn() {
    if (this.data.detailsData.is_like == 1) {
      wx.showToast({
        icon: "none",
        title: "你已赞过该贴",
        duration: 1500,
      });
      return false;
    }
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-invitation.like");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: { invitation_id: this.data.stickid },
      success: (resdata) => {
        var res = resdata.data;
        app.tips(res.msg);
        if (res.result != 1) return;
        this.setData({
          "detailsData.likeCount": this.data.detailsData.likeCount ? this.data.detailsData.likeCount + 1 : 1,
        });
      },
    });
  },
  subMainBtn() {
    if (!this.data.maincomment) {
      wx.showToast({
        icon: "none",
        title: "请输入你的精彩评论",
        duration: 1500,
      });
      return;
    }
    this.setData({
      mainCommentShow: false,
    });
    this.getReplyComments("main");
  },
  getReplyComments(getType) {
    let dataJson = {};
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-invitation.comment");
    dataJson.invitation_id = this.data.stickid;
    if (getType == "main") {
      dataJson.comment = this.data.maincomment;
    } else {
      dataJson.comment = this.data.replyContent;
    }
    if (getType == "reply") {
      if (this.data.host_comment_id) {
        dataJson.parent_id = this.data.host_comment_id;
      }
      if (this.data.second_comment_id) {
        dataJson.subordinate_id = this.data.second_comment_id;
      }
    }
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: dataJson,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.getStickComment();
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {},
    });
  },
  replySubmitBtn() {
    if (!this.data.replyContent) {
      wx.showToast({
        icon: "none",
        title: "请输入回复内容",
        duration: 1500,
      });
      return;
    }
    this.setData({
      replyshow: false,
    });
    this.getReplyComments("reply");
  },
  dminBtn(typenum) {
    let that = this;
    let dataJson = {};
    dataJson.tid = that.stickid;
    if (typenum == 1) {
      dataJson.operation = 1;
    } else if (typenum == 2) {
      dataJson.operation = 2;
    }
    let urlStr = app.getNetAddresss("plugin.micro-communities.api.trick.changeOperation");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: dataJson,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            adminOperationshow: false,
          });
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        } else {
          that.setData({
            adminOperationshow: false,
          });
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {},
    });
  },
  deleteBtn() {
    let that = this;
    let dataJson = {};
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-invitation.del");
    dataJson.invitation_id = that.data.stickid;
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: dataJson,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
          that.setData({
            releaseOperationshow: false,
            adminOperationshow: false,
          });
          wx.reLaunch({
            url: "/packageH/circleCommunity/circleIndex/circleIndex",
          });
        } else {
          that.setData({
            releaseOperationshow: false,
            adminOperationshow: false,
          });
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {},
    });
  },
  //获取圈子设置
  getCircleSet() {
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle.get-set");
    app._getNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.setData({
          reward_button: res.data.dashan,
        });
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  adCancel() {
    this.setData({
      adminOperationshow: false,
    });
  },
  reEditBtn() {
    wx.navigateTo({
      url: "/packageH/circleCommunity/circleTopic/circleTopic?stickId=" + this.data.stickid,
    });
  },
  reCancelBtn() {
    this.setData({
      releaseOperationshow: false,
    });
  },
  shorewardArrBtn(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      rewardArrmoneytype: 1,
      rewardArrselect: index,
      rewardArrmoney: "",
    });
  },
  confirmRewardBtn() {
    this.setData({
      amountRewardshow: false,
    });
    console.log(this.data.rewardArrmoneytype);
    if (this.data.rewardArrmoneytype == 1) {
      this.setData({
        rewardMoney: parseInt(this.data.rewardArr[this.data.rewardArrselect].name),
      });
    } else if (this.data.rewardArrmoneytype == 2) {
      this.setData({
        rewardMoney: this.data.rewardArrmoney,
      });
    }
    if (app._isTextEmpty(this.data.rewardMoney)) {
      wx.showToast({
        icon: "none",
        title: "请输入或选择打赏金额",
        duration: 1500,
      });
      return;
    }
    if (this.data.rewardMoney <= 0) {
      wx.showToast({
        icon: "none",
        title: "打赏金额必须大于0",
        duration: 1500,
      });
      return;
    }
    this.getPayData();
  },
  //获取支付类型参数
  getPayData() {
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-pay.get-pay-button");
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          let btnData = res.data.buttons;
          this.setData({
            btnData: btnData,
            rewardPayshow: true,
          });
        }
      },
      fail: (res)=> {
        console.log(res);
      },
    });
  },
  payBtn(e) {
    let type, name, btn = null;
    if (e.currentTarget) {
      type = e.currentTarget.dataset.info.value;
      name = e.currentTarget.dataset.info.name;
      btn = e.currentTarget.dataset.info;
    } else {
      type = e.value;
      name = e.name;
      btn = e;
    }

    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-pay.invitation-reward");
    let dataJson = {
      pay_method: type,
      price: this.data.rewardMoney,
      pay_name: name,
      invitation_id: this.data.stickid,
      client_type: "2",
    };
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: dataJson,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            rewardPayshow: false,
          });

          this.handleOtherPay(btn, type, res.data);
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
    });
  },
  rewardPayCancelBtn() {
    this.setData({
      rewardPayshow: false,
    });
  },
  replyBtn() {
    this.setData({
      replyshow: true,
      commentManagement: false,
    });
  },
  delBtn() {
    if (this.data.host_comment_id && this.data.second_comment_id) {
      this.setData({
        commend_id: this.data.second_comment_id,
      });
    } else {
      this.setData({
        commend_id: this.data.host_comment_id,
      });
    }
    this.setData({
      commentManagement: false,
    });
    this.delReply();
  },
  delReply() {
    let that = this;
    let dataJson = {};
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-invitation.delete-comment");
    dataJson.comment_id = this.data.commend_id;
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: dataJson,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.getStickComment();
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {},
    });
  },
  replyCancelBtn() {
    this.setData({
      commentManagement: false,
    });
  },
  imgHeight: function (e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    var imgh = e.detail.height; //图片高度
    var imgw = e.detail.width; //图片宽度
    var swiperH = (winWid * imgh) / imgw + "px"; //等比设置swiper的高度。 即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度  ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    if (parseInt(swiperH) > this.data.LastHeight) {
      this.setData({
        Height: swiperH, //设置高度
      });
      this.data.LastHeight = parseInt(swiperH);
    }
  },
  maincommentinp(e) {
    let val = e.detail.value;
    this.setData({
      maincomment: val,
    });
  },
  rewardArrmoneyinp(e) {
    let val = e.detail.value;
    this.setData({
      rewardArrmoney: val,
    });
  },
  replyContentinp(e) {
    let val = e.detail.value;
    this.setData({
      replyContent: val,
    });
  },
  replyfocus(e) {
    let height = e.detail.height;
    this.setData({
      replyheight: height,
    });
  },
  replyblur(e) {
    this.setData({
      replyheight: 0,
    });
  },
  replyshowClose() {
    this.setData({
      replyshow: false,
    });
  },
  mainCommentShowClose() {
    this.setData({
      mainCommentShow: false,
    });
  },
  adminOperationshowClose() {
    this.setData({
      adminOperationshow: false,
    });
  },
  releaseOperationshowClose() {
    this.setData({
      releaseOperationshow: false,
    });
  },
  amountRewardshowClose() {
    this.setData({
      amountRewardshow: false,
    });
  },
  rewardPayshowClose() {
    this.setData({
      rewardPayshow: false,
    });
  },
  commentManagementClose() {
    this.setData({
      commentManagement: false,
    });
  },
  getMoreData() {
    if (this.data.mulcommentsshow) {
      return false;
    }
    let json = {
      invitation_id: this.data.stickid,
    };
    this.data.isLoadMore = false; // 防止多次请求分页数据
    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-invitation.get-comment-list");
    if (this.data.page >= this.data.total_page) {
      this.setData({
        loading: true,
      });
    } else {
      this.data.page = this.data.page + 1;
      json.page = this.data.page;
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: json,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            var myData = res.data.data;
            this.setData({
              StickCommentData: this.data.StickCommentData.concat(myData),
            });
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        },
        fail: function (res) {},
      });
    }
  },
});
